package com.example.sensorapp;


import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.concurrent.atomic.AtomicReference;

public class Act1 extends Activity implements SensorEventListener {

    // 定义模拟器的Sensor管理器
    SensorManager mSensorManager;
    // 定义界面上的文本框组件
    TextView etTxt1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act1);

        // 获取程序界面的文本框组件
        etTxt1 = (TextView) findViewById(R.id.txt1);

        // 获取传感器模拟器的传感器管理服务
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);


    }

         @Override
        protected void onResume () {
            // 为系统的加速度传感器注册监听器
            mSensorManager.registerListener(this,mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) , SensorManager.SENSOR_DELAY_GAME);
            super.onResume();
        }
        @Override
        protected void onStop () {
            // 取消注册
            mSensorManager.unregisterListener(this);
            super.onStop();
        }

        @Override
        public void onSensorChanged (SensorEvent event){
            float[] values = event.values;
            StringBuilder sb = new StringBuilder();
            sb.append("X方向的加速度为：").append(values[0]).append("\n");
            sb.append("Y方向的加速度为：").append(values[1]).append("\n");
            sb.append("Z方向的加速度为：").append(values[2]).append("\n");
            etTxt1.setText(sb);
        }

        @Override
        public void onAccuracyChanged (Sensor sensor,int accuracy){

        }

    }